package com.t235.gc.device.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.t235.gc.AjaxResult;
import com.t235.gc.entity.Device;
import com.t235.gc.device.service.DeviceService;
import com.t235.gc.entity.DeviceCabinet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 设备表(Device)表控制层
 *
 * @author makejava
 * @since 2021-03-24 08:45:22
 */
@Api
@RestController
@RequestMapping("/api/device")
public class DeviceController {
    @Autowired
    DeviceService deviceService;

    @ApiOperation(value = "获取顶端的设备数量", notes = "根据设备状态获取设备数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "设备状态", required = true, defaultValue = "0"),
            @ApiImplicitParam(name = "isStock", value = "是否缺货", required = true, defaultValue = "0")
    })
    @GetMapping("/admin/getBackDeviceCountStatistics")
    public AjaxResult getBackDeviceCountStatistics(@RequestParam(defaultValue = "0") Integer status, @RequestParam(defaultValue = "0") Integer isStock) {
        return AjaxResult.success(deviceService.selectDeviceStatusCount(status, isStock));
    }

    @GetMapping("/admin/getBackDeviceList")
    @ApiOperation(value = "获取设备列表", notes = "根据设备信息获取设备列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "device", value = "设备信息", required = true),
            @ApiImplicitParam(name = "date", value = "创建时间", required = true),
            @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, defaultValue = "0"),
            @ApiImplicitParam(name = "pageSize", value = "页面显示数量", required = true, defaultValue = "2")
    })
    public AjaxResult getBackDeviceList(Device device, String createTime, String faultTime, @RequestParam(value = "page", defaultValue = "0") Integer pageNo, @RequestParam(value = "limit", defaultValue = "2") Integer pageSize) throws Exception {
        if (createTime != null && createTime != "") {
            String[] dates = createTime.split(" - ");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            device.setCtime(format.parse(dates[0]));
            device.setOfflinetime(format.parse(dates[1]));
        }
        if (faultTime != null && createTime != "") {
            String[] dates = faultTime.split(" - ");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            device.setFaulttime(format.parse(dates[0]));
            device.setOfflinetime(format.parse(dates[1]));
        }
        PageHelper.startPage(pageNo, pageSize);
        List<Device> cs = deviceService.selectDevicePageBean(device);
        PageInfo<Device> pageInfo = new PageInfo<>(cs);
        return AjaxResult.success(pageInfo);
    }

    @ApiOperation(value = "新增或修改设备信息", notes = "新增或修改设备信息")
    @ApiImplicitParam(name = "device", value = "设备信息", required = true)
    @PostMapping("/admin/saveOrUpdateDevice")
    public AjaxResult saveOrUpdateDevice(Device device, DeviceCabinet deviceCabinet, String position) {
        if (position != null && position != "") {
            String[] positions = position.split("/");
            device.setProvince(positions[0]);
            device.setCity(positions[1]);
            device.setArea(positions[2]);
        }
        return AjaxResult.success(deviceService.saveOrUpdateDevice(device, deviceCabinet));
    }

    @ApiOperation(value = "删除设备", notes = "删除设备")
    @ApiImplicitParam(name = "id", value = "删除设备id或设备id列表", required = true)
    @DeleteMapping("/admin/delDevice")
    public AjaxResult delDevice(Long id) {
        return AjaxResult.success(deviceService.delDevice(id));
    }

    @PutMapping("/admin/deviceVersionUpgrade")
    public AjaxResult deviceVersionUpgrade(Long[] id, Integer versionId) {
        Integer i = 0;
        for (Long a : id) {
            i = deviceService.deviceVersionUpgrade(a, versionId);
        }
        return AjaxResult.success(i);
    }

    @PutMapping("/admin/addDevice")
    public AjaxResult addDevice(Device device, DeviceCabinet deviceCabinet) {
        return AjaxResult.success(deviceService.addDevice(device, deviceCabinet));
    }
}
